package com.qf.service.impl;

import com.qf.entity.Pie;
import com.qf.mapper.CartDao;
import com.qf.mapper.OrdersDao;
import com.qf.mapper.UserDao;
import com.qf.service.ReportService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Service
public class  ReportServiceImpl implements ReportService {

    private UserDao userDao;
    private CartDao cartDao;
    private OrdersDao ordersDao;

    public ReportServiceImpl(UserDao userDao, CartDao cartDao, OrdersDao ordersDao){
        this.userDao = userDao;
        this.cartDao = cartDao;
        this.ordersDao =  ordersDao;
    }

    @Override
    public Map<String, List> getPieAll(Integer n) {
        //报表汇总
        Map<String, List> pieAll = new HashMap<>();
        //1.
        //今日新增用户数据
        List<Map<String, Integer>> listUser = userDao.getAddUser();
        List<Pie> addUserPie = getReport(listUser);
        //2.
        //今日销售额统计
        List<Map<String, Integer>> listTodayMoney = ordersDao.getTodayMoney();
        List<Pie> todayMoney = getReport(listTodayMoney);
        //3.5.
        //各类商品销售额统计、占比
        List<Pie> goodsMoney = ordersDao.getItemMoney();
        //4.
        //用户购物车前十商品统计
        List<Pie> topTen = cartDao.getGoodsNum();
        //6.
        //自增用户曲线
        List<Map<String, Object>> nDayUser = userDao.getNDayUser(n);
        List<Pie> addUser = new ArrayList<>();
        Map<Object,Object> map = new HashMap<>();
        //数据处理
        nDayUser.forEach(x -> {
            map.put(x.get("name"),x.get("value"));
        });
        //报表生成

        LocalDate nday;
        Date date;
        for(int i = n; i >= 0; i--){
            nday = LocalDate.now().minusDays(i);
            date = Date.from(nday.atStartOfDay(ZoneId.systemDefault()).toInstant());
            if(map.get(date) == null){
                addUser.add(new Pie(nday.toString(),new BigDecimal(0)));
            }else {
                BigDecimal num = new BigDecimal(map.get(date).toString());
                addUser.add(new Pie(nday.toString(),num));
            }
        }

        //报表汇总
        pieAll.put("todayUser",addUserPie);
        pieAll.put("todayMoney",todayMoney);
        pieAll.put("goodsMoney",goodsMoney);
        pieAll.put("topTen",topTen);
        pieAll.put("addUser",addUser);

        return pieAll;
    }

    //时间类报表生成方法
    private List<Pie> getReport(List<Map<String, Integer>> listMap){
        List<Pie> list = new ArrayList<>();
        Map<Integer,Integer> map = new HashMap<>();
        //数据处理
        listMap.forEach(x -> {
            map.put(x.get("name"),x.get("value"));
        });
        //今日新增用户报表生成
        Number vlaue;
        BigDecimal sum = new BigDecimal(0);
        for (Integer i = 0 ; i < 24 ; i++){
            vlaue = map.get(i);
            if(vlaue == null){
                //时间未到值为空
                if(i > Calendar.getInstance().get(Calendar.HOUR_OF_DAY)){
                    list.add(new Pie(i+":00",null));
                }else {
                    list.add(new Pie(i+":00",sum));
                }
            }else {
                BigDecimal num = new BigDecimal(vlaue.toString());
                sum = sum.add(num);
                list.add(new Pie(i+":00",sum));
            }
        }
        return list;
    }
}
